package com.qingke.pms.command.user;

import java.util.List;

import com.qingke.pms.command.UserCommand;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class ListUsersCommand extends UserCommand {

	public ListUsersCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		while(true){
			PMSConsole.println("1.all 2.id 3.name(like) 4.email(like) 5.phone 6.exit");
			String cmd=PMSConsole.promptNotEmpty("list user>").trim().toLowerCase();
			switch(cmd){
			case "1":
			case "all":
				all();
				continue;
			case "2":
			case "id":
				id();
				continue;
			case "3":
			case "name":
				name();
				continue;
			case "4":
			case "email":
				email();
				continue;
			case "5":
			case "phone":
				phone();
				continue;
			case "6":
			case "exit":
				break;
			default:
				PMSConsole.println("err input.");
				continue;
			}
			break;
		}
	}
	
	private void all(){
		List<User> list=dao.selectAllUsers();
		if(list.size()==0){
			PMSConsole.println("no user");
			return ;
		}
		PMSConsole.println("-------all users-------");
		for(User u:list){
			PMSConsole.println(u);
		}
		PMSConsole.println();
	}
	
	private void id(){
		int id=PMSConsole.getInt("user id:");
		if(!dao.isUserExistById(id)){
			PMSConsole.println("the user is not existed.");
			return ;
		}
		User user=dao.selectUserById(id);
		PMSConsole.println(user);
	}

	private void name(){
		String name=PMSConsole.promptCanEmpty("name like:");
		String nameLike="%"+name+"%";
		List<User> list=dao.selectUsersByLikeName(nameLike);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------users name like:"+name+"-------");
		for(User u:list){
			PMSConsole.println(u);
		}
		PMSConsole.println();
	}
	
	private void email(){
		String email=PMSConsole.promptCanEmpty("email like:");
		String emailLike="%"+email+"%";
		List<User> list=dao.selectUsersByLikeEmail(emailLike);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------users email like:"+email+"-------");
		for(User u:list){
			PMSConsole.println(u);
		}
		PMSConsole.println();
	}
	
	private void phone(){
		String phone=PMSConsole.promptCanEmpty("phone like:");
		String phoneLike="%"+phone+"%";
		List<User> list=dao.selectUsersByLikePhone(phoneLike);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------users phone like:"+phone+"-------");
		for(User u:list){
			PMSConsole.println(u);
		}
		PMSConsole.println();
	}
}
